package com.woniuxy.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.Dish;
import com.woniuxy.entity.Manager;
import com.woniuxy.entity.Order;
import com.woniuxy.entity.OrderItem;
import com.woniuxy.mapper.DishMapper;
import com.woniuxy.mapper.ManagerMapper;
import com.woniuxy.mapper.OrderItemMapper;
import com.woniuxy.mapper.OrderMapper;
import com.woniuxy.service.ManagerService;
import com.woniuxy.service.OrderService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
    * @description:获取全部订单
     * @return: java.util.List<com.woniuxy.entity.Order>
     * @date: 2021/8/12 14:40
     * @author: 魏炳豪
     */
    @Override
    public List<Order> getAllOrders() {
        //获取全部order
        List<Order> orders = orderMapper.getAllOrders();

        orders.forEach(p->{
           //根据订单id获取对应订单详情
            List<OrderItem> orderItems = orderItemMapper.getByOrderId(p.getId());
            orderItems.forEach(o->{
                //根据菜品id获取菜品
                Dish dish = dishMapper.selectById(o.getDishId());
                o.setDish(dish);

            });
            //根据订单详情中的dishId获取菜品信息
            p.setOrderItems(orderItems);
        });
        return orders;
    }

    /**
    * @description: 根据条件查询订单
     * @param order:
     * @return: java.util.List<com.woniuxy.entity.Order>
     * @date: 2021/8/12 14:40
     * @author: 魏炳豪
     */
    @Override
    public List<Order> getByCondition(Order order) {

        return orderMapper.getByCondition(order);
    }

    @Override
    public void update(String number,int userid,int status){
        orderMapper.update(number,userid,status);
    }

    @Override
    public void update1(int id, String isReview,Integer status) {
        orderMapper.update1(id,isReview,status);
    };

    @Override
    public void add(Order order, List<OrderItem> orderItem){
        orderMapper.insert(order);
        for (OrderItem item : orderItem) {
            orderItemMapper.insert(item);
        }
    };
}
